<?php
 require_once ROOT_PATH . '/lib/models/surveys/Questions.php';
 require_once ROOT_PATH . '/lib/models/surveys/Answers.php';
 require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';
?>

<h2><?php echo $records['survey']->getSurveyName(); ?><hr/></h2>
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
    	<th class="tableTopMiddle" width= "600"></th>    
		<th class="tableTopRight"></th>
	</tr>
	
  </thead>
  <tbody>
<?php 
	$j = 0;
	$index =1;
	$questionArr=null;
	
	if (is_array($records['questions']) && count($records['questions'])>0) {
		$options =null;		
		$totalscore = 0;
		$personalScore =0;
		foreach ($records['questions'] as $record) {
			if($record->getAnswerType() != 4)
				$totalscore ++;			
			if(!($j%2)) { $cssClass = 'odd';
				} else {  	$cssClass = 'even';  }						 
				
				if(( $record->getAnswerType() == 2 || $record->getAnswerType() == 3 )){
					$options = $record->getOptions($record->getId());
					if($record->getAnswerType() == 3){
						$type = "type='checkbox'";
						$name ="[]";
					}
					else
					{
						$type = "type='radio'";
						$name ='';
					}
					?>
					<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>" style="font-weight:bold;"><?php echo "Question ".$index." :". $record->getText(); ?></td>
						<td class="tableMiddleRight"></td>
			  		</tr>
			  		<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>">
					    	<table>
					    		<tr>
					    		<?php 
					    			$k=0;
					    			$totalRightAnswer = 0;
					    			$rightAnswer = 0;
					    			$wrongAnswer = 0;
					    			foreach ($options as $option) {	
					    			if($option->getValue()==1)
					    				$totalRightAnswer++;				    			
					    			$checkedOptions = Answers::getAnswersByOptionQuestionSurvey($records['survey']->getId(),$record->getId(),$option->getId(),$_SESSION['empID']);
					    			$checkedOptionsId = array();
					    			foreach($checkedOptions as $item){
					    				$checkedOptionsId[$k]= $item->getValue();
					    				$k++;
					    			}
					    		?>	    		
					    			<td>
					    			<?php if(in_array($option->getId(),$checkedOptionsId)&& $option->getValue()==1 ) {  ?>
					    				<input <?php echo $type; ?> checked="checked" name='<?php echo $record->getId().$name; ?>' value='<?php echo $option->getId(); ?>' /><?php echo "<span style='color:green;'>".$option->getText()."</span>"; ?>
					    			<?php $rightAnswer++;   }else if(in_array($option->getId(),$checkedOptionsId)&& $option->getValue()!=1 ) { ?>
					    				<input <?php echo $type; ?> checked="checked" name='<?php echo $record->getId().$name; ?>' value='<?php echo $option->getId(); ?>' /><?php echo "<span style='color:red;'>".$option->getText()."</span>"; ?>
					    			<?php $wrongAnswer++;}else { ?>
					    				<input <?php echo $type; ?>  name='<?php echo $record->getId().$name; ?>' value='<?php echo $option->getId(); ?>' /><?php echo $option->getText(); ?>
					    			<?php }?>		
					    			<td>	    		
					    		<?php } 
					    		if($totalRightAnswer==0)
					    			$personalScore += 0;
					    		else{
					    			if(round($rightAnswer/$totalRightAnswer,1)== 1 && $wrongAnswer ==0)
					    				$personalScore += 1;
					    		}
					    			
					    		?>
					    		</tr>
					    	</table>
				    	</td>
						<td class="tableMiddleRight"></td>
			  		</tr>	
					<?php
					$questionArr[$j]=$record->getId();
			 		$j++;
			 		$index++;
		 		}else if($record->getAnswerType() == 1){ ?>
		 			<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>" style="font-weight:bold;"><?php echo "Question ".$index." :".$record->getText(); ?></td>
						<td class="tableMiddleRight"></td>
			  		</tr>
			  		<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>">
					    	<table>
					    		<tr>
					    			<td>
					    			<input type='radio' name='<?php echo $record->getId(); ?>' value ="1" />Yes
					    			<td>
					    			<td>
					    			<input type='radio' name='<?php echo $record->getId(); ?>' value="0" />No
					    			<td>	    		
					    		</tr>
					    	</table>
				    	</td>
						<td class="tableMiddleRight"></td>
			  		</tr>	
		 		<?php
		 		$questionArr[$j]=$record->getId();
		 		$j++;
		 		$index++; 
		 		}else if($record->getAnswerType() == 4){ 
		 			$customAnswers = Answers::getCustomAnswersBySurveyUser($record->getSurveyId(),$record->getId(),$records['userid']);
		 			if(count($customAnswers)>0){
		 			$customAnswers = $customAnswers[0];
		 		?>
		 			<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>" style="font-weight:bold;"><?php echo "Question ".$index." :".$record->getText(); ?></td>
						<td class="tableMiddleRight"></td>
			  		</tr>
			  		<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>">
					    	<TEXTAREA  id="<?php echo $record->getId(); ?>" name="<?php echo $record->getId(); ?>"  COLS="70" ROWS="1"/><?php echo $customAnswers->getValue(); ?></TEXTAREA>
				    	</td>
						<td class="tableMiddleRight"></td>
			  		</tr>	
		 		<?php
		 		$questionArr[$j]=$record->getId();
		 		$j++; 
		 		$index++;
		 		}
		 		}
			}
	}
	else{ ?>
	<tr>
	  	<td class="tableMiddleLeft"></td>
	    <td class=""><?php echo $lang_Start_Survey_No_Question; ?></td>
		<td class="tableMiddleRight"></td>
	</tr>
			
		<?php }
	?>
	<tr>
	  	<td class="tableMiddleLeft"></td>
	    <td class=""><span style="font-weight:bold;color:red;">Red:</span><span > Wrong answer</span></td>
		<td class="tableMiddleRight"></td>
	</tr>	
	<tr>
	  	<td class="tableMiddleLeft"></td>
	    <td class=""><span style="font-weight:bold;color:green;">Green:</span><span > Right answer</span></td>
		<td class="tableMiddleRight"></td>
	</tr>	
  	<tr>
	  	<td class="tableMiddleLeft"></td>
	    <td class=""><span style="font-weight:bold;color:black;font-size:13px;"> Your score is <?php echo $personalScore; ?> over <?php echo $totalscore; ?> points! </span></td>
		<td class="tableMiddleRight"></td>
	</tr>	
  </tbody>
  <tfoot>  	
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>			
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>
</table>